package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.base.response.JsonResult;
import cn.tedu._05mvcboot02.base.response.StatusCode;
import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.bto.InsertDTO;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import cn.tedu._05mvcboot02.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

//@Controller

@Slf4j
@RestController
@Api(value = "用户模块")
@RequestMapping("/v1/users/")
public class UserController {

    @Autowired
    private UserMapper userMapper;

//    @ResponseBody
    @ApiOperation(value = "添加用户功能")
    @PostMapping("insert")
    public JsonResult insert(InsertDTO insertDTO){
        log.debug("insertDTO = {}", insertDTO);
        User user = new User();
        BeanUtils.copyProperties(insertDTO ,user);
        user.setCreated(new Date());
        userMapper.insert(user);
        return JsonResult.ok();
    }

//    @ResponseBody
    @ApiOperation(value = "用户列表功能")
    @GetMapping("userList")
    public JsonResult userList(){
        List<UserListVO> userListVOList= userMapper.userList();
        return JsonResult.ok(userListVOList);
    }

//    @ResponseBody
    @ApiOperation(value = "删除指定用户功能")
    @GetMapping("delete")
    @ApiImplicitParam(name = "id" ,value = "用户编号",required = true,dataType = "int")
    public JsonResult deleteUser(Integer id){
        int i = userMapper.deleteUser(id);
        if (i>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }

//    @ResponseBody
    @ApiOperation(value = "更新用户功能")
    @PostMapping("update")
    public JsonResult updateUser(User user){
        int i = userMapper.updateUser(user);
        if (i>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @ApiOperation(value = "查询指定用户功能")
    @GetMapping("selectById/{uid}")
    @ApiImplicitParam(name = "uid",value = "用户ID",required = true,dataType = "int")
    public JsonResult selectById(@PathVariable Integer uid){
        UserVO userVO = userMapper.selectById(uid);
        return JsonResult.ok(userVO);
    }

}
